package com.web.admin.web.base.wechat;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.wechat.facade.WechatFacade;
import com.web.base.wechat.facade.WechatMenuFacade;
import com.web.base.wechat.message.WechatMenuRequest;
import com.web.base.wechat.message.WechatMenuResponse;
import com.web.base.wechat.message.WechatResponse;
import com.web.core.async.redis.CacheRefreshSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

@Controller
@Http(url = "/base/wechat")
public class WechatMenuController
{
	@Autowired
	private WechatMenuFacade wechatMenuFacade;
	@Autowired
	private WechatFacade wechatFacade;
	@Resource
	private CacheRefreshSender cacheRefreshSender;
	
	private DictParser dictParser = DictParser.getInstance();
	
	public WechatMenuController()
	{
	}
	
	@Http(url = "/wechat_menu_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		WechatMenuRequest wechatMenuRequest = request.getQueryObject(WechatMenuRequest.class);
		List<WechatResponse> wechatList = wechatFacade.listAll();
		if (wechatMenuRequest.getWechatId() == null && !wechatList.isEmpty())
		{
			wechatMenuRequest.setWechatId(wechatList.get(0).getId());
		}
		return View.createBuilder().setView("/base/wechat/wechat_menu_list")
				.put("wechatList", wechatList)
				.put("map", dictParser.getStringMap())
				.put("model", wechatMenuRequest)
				.put("treeList", wechatMenuFacade.listMenu(wechatMenuRequest.getWechatId()))
				.build();
	}
	
	@Http(url = "/wechat_menu_get", method = HttpMethod.GET)
	public View get(HttpRequest request)
	{
		WechatMenuRequest wechatMenuRequest = request.getQueryObject(WechatMenuRequest.class);
		List<WechatResponse> wechatList = wechatFacade.listAll();
		WechatMenuResponse response = null;
		if (wechatMenuRequest.getId() != null)
		{
			response = wechatMenuFacade.getAll(wechatMenuRequest.getId());
		}
		if (response == null)
		{
			response = new WechatMenuResponse();
			response.setWechatId(wechatMenuRequest.getWechatId());
			response.setParentId(wechatMenuRequest.getParentId());
			if (response.getParentId() != null)
			{
				WechatMenuResponse parent = wechatMenuFacade.get(response.getParentId());
				if (parent != null)
				{
					response.setParentName(parent.getName());
				}
			}
		}
		return View.createBuilder().setView("/base/wechat/wechat_menu_get")
				.put("wechatList", wechatList)
				.put("model", response)
				.put("map", dictParser.getStringMap())
				.put("treeList", wechatMenuFacade.listRootMenu())
				.build();
	}
	
	@Http(url = "/wechat_menu_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		WechatMenuRequest wechatMenuRequest = request.getPostObject(WechatMenuRequest.class);
		WechatMenuResponse response = null;
		if (wechatMenuRequest.getId() == null)
		{
			ValidationUtil.valid(wechatMenuRequest, SaveModel.class);
			response = wechatMenuFacade.add(wechatMenuRequest);
		}
		else
		{
			ValidationUtil.valid(wechatMenuRequest, UpdateModel.class);
			response = wechatMenuFacade.update(wechatMenuRequest);
		}
	}
	
	@Http(url = "/wechat_menu_sync", method = HttpMethod.POST)
	public void sync(HttpRequest request)
	{
		String code = request.getPostMap().get("code");
		//wechatInvokerFacade.syncMenu(code);
	}
	
}
